/**
 * 机构数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.SysTableGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	initComponent: function(){
		var gridStructure = [{ 
			name: 'id',
			hidden: true
		},{
			name: 'tableName',  
			header: "表名",
			sortable: true,
			width: 150
		},{
			name: 'tableChineseComments',  
			header: "表注释",
			sortable: true,
			width: 200
		},{
			name: 'owner',  
			header: "所属用户",
			width: 150
		},{
			name: 'memo',  
			header: "描述",
			width: 300
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'tableName';
		this.keyField = 'id';

		this.deleteMsg = '确认要删除选中的表吗？<br/><font color="red">注意</font>：删除表将删除该表下的所有字段。';
		this.deleteMsgBoxWidth = 350;
		
		Ext.ux.grid.SysTableGrid.superclass.initComponent.call(this);
		
		this.on('rowdblclick', function(grid){
			var record = grid.getSelectionModel().getSelected();
			if(record != null){
				var id = record.get('id');//解析
				this.showTableInfo(id);
			}
		});
		
		this.on('updateRecord', function(tableId){
			this.showTableInfo(tableId);
		});
		
		this.on('deleteRecord', function(grid, tableIds){
			this.deleteTable(tableIds);
		});
	},
	
	showTableInfo: function(tableId){
		var win = new Ext.ux.sysmeta.SysTableDetailWindow({id:'tableDetailWindow', title:'修改表信息',tableId:tableId});
		win.showTableInfo();
	},
	
	addTable: function(){
		var win = new Ext.ux.sysmeta.SysTableDetailWindow({id:'tableDetailWindow', title:'新增表'});
		win.show();
	},
	
	deleteTable: function(tableIds) {
		Ext.Ajax.request({
			url:'sys-meta-table!deleteMore.action',
			success: function(){
				Ext.getCmp('treePanel').refresh();
				this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除表失败！');
			},
			method: 'POST',
			params:{
				ids: tableIds
			},
			waitMsg : '正在删除数据，稍候...',
			scope: this
		});
	}
});